﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";
    var dayInfo, preYear, preMonth, preDay;
    var fullcommondate, fulllunardate, lunarday, gzyear, gzmonth, goodInfo, badInfo;
    WinJS.UI.Pages.define("/pages/DayView/DayView.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {

            if (!options) {
                dayInfo = display(currentDay);
            }
            else {
                dayInfo = display(options.daynumber)
            }

            if (dayInfo) {
                fullcommondate = dayInfo.date.sYear + "年" + dayInfo.date.sMonth + "月" + dayInfo.date.sDay + "号&nbsp;&nbsp;" + "星期" + dayInfo.date.week + "" + dayInfo.date.solarFestival + dayInfo.date.solarTerms + "  " + dayInfo.date.lunarFestival
                fulllunardate = "";
                lunarday = "农历 " + /*dayInfo.date.lYear + "年" + */monthName[(dayInfo.date.lMonth - 1)] + cDay(dayInfo.date.lDay);
                gzyear = dayInfo.gzYear + "年     " + "[" + dayInfo.animal + "]" + "年";
                gzmonth = dayInfo.gzMonth + "月     " + dayInfo.gzDay + "日";
                this.setValue("fullcommondate", fullcommondate);
                this.setValue("fulllunardate", "");
                this.setValue("daynumber", dayInfo.date.sDay);
                this.setValue("lunarday", lunarday);
                this.setValue("gzyear", gzyear);
                this.setValue("gzmonth", gzmonth);

                var goodSection = this.getElement("goodcontent");
                goodInfo = this.appendContent(goodSection, dayInfo.y);
                var badSection = this.getElement("badcontent");
                badInfo = this.appendContent(badSection, dayInfo.j);

                this.setValue("horoscopecontent", dayInfo.horoscope);
            }

            //share
            var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
            dataTransferManager.addEventListener("datarequested", dataRequested);
            document.getElementById("sharebutton").addEventListener("click", shareData, false);

            this.getElement("preday").addEventListener("click", goToPreviousDay, false);
            this.getElement("nextday").addEventListener("click", goToNextDay, false);
        },

        unload: function () {
            var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
            dataTransferManager.removeEventListener("datarequested", dataRequested);
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        },
        appendContent: function (parent, data) {
            var dataList = data.split(".");
            var count = 0;
            var info = "", i;
            for (i = 0; i < dataList.length; i++) {
                if (count >= 10) break;
                if (dataList[i] !== "") {
                    var divTag = document.createElement("div")
                    divTag.innerHTML = dataList[i].trim();
                    parent.appendChild(divTag);
                    info += dataList[i].trim();
                }
                count++;
            }
            return info;
        },
        setValue: function (id, data) {
            this.getElement(id).innerHTML = data;;
        },
        getElement: function (id) {
            return document.getElementById(id);
        }
    });

    function goToPreviousDay() {
        var d = getPreviousDate(getCurrentDate());
        currentYear = d.getFullYear();
        currentMonth = d.getMonth();
        currentDay = d.getDate();
        WinJS.Navigation.navigate("/pages/DayView/DayView.html", { "daynumber": currentDay });
    }

    function goToNextDay() {
        var d = getNextDate(getCurrentDate());
        currentYear = d.getFullYear();
        currentMonth = d.getMonth();
        currentDay = d.getDate();
        WinJS.Navigation.navigate("/pages/DayView/DayView.html", { "daynumber": currentDay });
    }

    function getCurrentDate() {
        return new Date(dayInfo.date.sYear, dayInfo.date.sMonth - 1, dayInfo.date.sDay);
    }

    function getPreviousDate(d) {
        return new Date(new Date(d.getTime() - (24 * 60 * 60 * 1000)));
    }

    function getNextDate(d) {
        return new Date(new Date(d.getTime() + (24 * 60 * 60 * 1000)));
    }


    function dataRequested(e) {
        var request = e.request;

        // Title is required
        var dataPackageTitle = "[分享]" + dayInfo.date.sYear + "年" + dayInfo.date.sMonth + "月" + dayInfo.date.sDay + "号";
        if ((typeof dataPackageTitle === "string") && (dataPackageTitle !== "")) {
            var dataPackageText = fulllunardate + lunarday + gzyear + gzmonth + "  宜：" + goodInfo + "  忌：" + badInfo;
            if ((typeof dataPackageText === "string") && (dataPackageText !== "")) {
                request.data.properties.title = dataPackageTitle;

                // The description is optional.
                var dataPackageDescription = "来自[中国好日历]";
                if ((typeof dataPackageDescription === "string") && (dataPackageDescription !== "")) {
                    request.data.properties.description = dataPackageDescription;
                }
                request.data.setText(dataPackageText);
            } else {
                request.failWithDisplayText("Enter the text you would like to share and try again.");
            }
        } else {
            request.failWithDisplayText(SdkSample.missingTitleError);
        }
    }

    function shareData() {
        Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();
    }

})();
